#include "Cards.h"

Card::Card()
{
	m_itype = 0;
}
Card::Card(int type)
{
	m_itype = type;
}
Card::~Card()
{
	delete &m_itype;
}

void Deck::createDeck(char* nameofFile)
{
	ostringstream file;
	string snumofcards;
	string stype;
	m_cpdeckfile = nameofFile;
	file << "Resources/" << m_cpdeckfile;
	ifstream TheFile(file.str());
	if(TheFile.is_open())
	{
		int decksize = 0;
		while(!TheFile.eof())
		{
			getline(TheFile, snumofcards, ' ');
			getline(TheFile, stype);

			istringstream numbuffer(snumofcards);
			int numofcards = 0;
			numbuffer >> numofcards;

			istringstream typebuffer(stype);
			int type = 0;
			typebuffer >> type;
			for (int i = 0; i < numofcards; ++i)
			{
				Card* tempcard = new Card(type);
				m_Deck.push_back(tempcard);
				m_deckindexer.add(decksize);
				decksize++;
			}
		}
	}
}
Card* Deck::drawCard()
{
	if(m_deckindexer.getSize() > 0)
	{
		int pull = rand() % m_deckindexer.getSize();
		Card* tempcard = m_Deck.at(m_deckindexer.get(pull));
		m_deckindexer.remove(m_deckindexer.get(pull));
		return tempcard;
	}
	else
	{
		deleteDeck();
		createDeck(m_cpdeckfile);
		return drawCard();
	}
}
void Deck::deleteDeck()
{
	m_Deck.empty();
	m_deckindexer.empty();
}